package com.springboot.demo.controller;

import com.springboot.demo.exception.ErrorPageException;
import com.springboot.demo.utils.ResponseResult;
import io.swagger.annotations.Api;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;

/**
 * @author wubc683
 * @desc 错误页面处理
 * @date 2021-08-17 14:14
 */
@Controller
@Api(tags = "错误页面接口")
public class CustomErrorPageController implements ErrorController {
    @RequestMapping("/error")
    public void errorPathHandler(HttpServletResponse response) {
        //抛出ErrorPageException异常，方便被ExceptionHandlerConfig处理
        ResponseResult result;
        switch (response.getStatus()) {
            case 400:
                result = ResponseResult.BAD_REQUEST;
                break;
            case 401:
                result = ResponseResult.UNAUTHORIZED;
                break;
            case 403:
                result = ResponseResult.FORBIDDEN;
                break;
            case 404:
                result = ResponseResult.PAGE_NOT_FOUND;
                break;
            case 405:
                result = ResponseResult.METHOD_NOT_ALLOWED;
                break;
            default:
                result = ResponseResult.SYSTEM_ERROR;
                break;
        }
        throw new ErrorPageException(result.getCode(),result.getMessage());
    }
}